package com.boc.ewr.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.CaExtranetList;
import com.boc.ewr.bcimp.entity.MacAddrRpt;
import com.boc.ewr.bcimp.service.ICaExtranetListService;
import com.boc.ewr.bcimp.service.IMacAddrRptService;
import com.boc.ewr.risk.entity.ExtranetCaFdbk;
import com.boc.ewr.risk.entity.IntranetCaFdbk;
import com.boc.ewr.risk.mapper.ExtranetCaFdbkMapper;
import com.boc.ewr.risk.service.IExtranetCaFdbkService;
import com.boc.ewr.util.MsgCategoryEnum;
import com.boc.ewr.util.ToUserUtil;
import com.boc.ewr.util.file.enumeration.YnEnum;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description: 外网对账数据反馈清单
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Service
public class ExtranetCaFdbkServiceImpl extends ServiceImpl<ExtranetCaFdbkMapper, ExtranetCaFdbk> implements IExtranetCaFdbkService {

    @Autowired
    private IMacAddrRptService macAddrRptService;

    @Autowired
    private ICaExtranetListService caExtranetListService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    /**
     * 导入
     * “外网对账数据”，“外网对账IP及MAC地址”
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createInfo() {
        // 清空当前记录
        //this.remove(null);

        // 生成数据
        List<CaExtranetList> caExtranetLists = caExtranetListService.list();
        List<ExtranetCaFdbk> extranetCaFdbkList = new ArrayList<>();
        for (CaExtranetList caExtranetList : caExtranetLists) {
            ExtranetCaFdbk extranetCaFdbk = new ExtranetCaFdbk();
            BeanUtils.copyProperties(caExtranetList, extranetCaFdbk);
            extranetCaFdbk.setId(null);

            DictModel parentDepartId = sysBaseAPI.getParentDepartId(extranetCaFdbk.getAdminOrgNo());
            extranetCaFdbk.setAdminOrgNo(parentDepartId.getValue());
            extranetCaFdbk.setCheckOrgNm(parentDepartId.getText());

            // 前期排查情况记录
            List<ExtranetCaFdbk> extranetCaFdbkListByMac = this.list(
                    new LambdaQueryWrapper<ExtranetCaFdbk>()
                            .eq(ExtranetCaFdbk::getMacAddr, extranetCaFdbk.getMacAddr())
                            .orderByDesc(ExtranetCaFdbk::getHandlerTime)
            );
            if (extranetCaFdbkListByMac.size() != 0) {
                extranetCaFdbk.setCheckRecordMac(extranetCaFdbkListByMac.get(0).toString());
            }

            // mac地址是否报备
            List<MacAddrRpt> macAddrRpts = macAddrRptService.list(
                    new LambdaQueryWrapper<MacAddrRpt>()
                            .eq(MacAddrRpt::getMacAddr, extranetCaFdbk.getMacAddr()
                            ));
            String isMacReport = "";
            if (macAddrRpts.size() != 0) {
                isMacReport = YnEnum.YES.getIndex();
            } else {
                isMacReport = YnEnum.NO.getIndex();
            }
            extranetCaFdbk.setIsMacReport(isMacReport);
            extranetCaFdbkList.add(extranetCaFdbk);
        }
        // 触发消息推送
        this.sendMsg(extranetCaFdbkList);

        return this.saveBatch(extranetCaFdbkList);
    }

    private void sendMsg(Collection<ExtranetCaFdbk> entityList) {

        Set<String> orgSet = new HashSet();
        for (ExtranetCaFdbk extranetCaFdbk : entityList) {
            orgSet.add(extranetCaFdbk.getOrgNo());
        }

        // 信息推送至网点对账人员
        String toUser = ToUserUtil.genToUser(orgSet);
        if (toUser != null) {
            MessageDTO messageDTO = new MessageDTO(
                    "system",
                    toUser,
                    "外网对账数据反馈",
                    "您有新的外网对账数据反馈任务，请及时查看并完成排查！",
                    MsgCategoryEnum.RISK_CHECK.getIndex());
            sysBaseAPI.sendSysAnnouncement(messageDTO);
        }

    }
}
